What is lodash.template?
The lodash.template package is a part of the Lodash library which provides utility functions for common programming tasks using the functional programming paradigm. It is specifically designed for creating compiled templates which can be used to interpolate values into strings.
What are lodash.template's main functionalities?
Template Compilation
Compiles templates into functions that can be used to interpolate values. The 'template' function takes a string and returns a compiled template function.
const _ = require('lodash.template');
const compiled = _.template('hello <%= user %>!');
console.log(compiled({ 'user': 'fred' }));
// => 'hello fred!'
HTML Escaping
Supports HTML escaping. The '<%-' sequence in the template string is used to escape values that are interpolated, preventing XSS attacks.
const _ = require('lodash.template');
const compiled = _.template('<b><%- value %></b>');
console.log(compiled({ 'value': '<script>' }));
// => '<b><script></b>'
Use of JavaScript Expressions
Allows the use of JavaScript expressions inside templates. This enables iteration and conditional logic within the template string.
const _ = require('lodash.template');
const compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
console.log(compiled({ 'users': ['fred', 'barney'] }));
// => '<li>fred</li><li>barney</li>'
Custom Delimiters
Supports custom delimiters. Users can define their own delimiters for interpolation, evaluation, and escaping.
const _ = require('lodash.template');
const compiled = _.template('hello ${ user }!', { 'interpolate': /\${([\s\S]+?)}/g });
console.log(compiled({ 'user': 'pebbles' }));
// => 'hello pebbles!'
Other packages similar to lodash.template
handlebars
Handlebars is a popular templating engine that is more feature-rich than lodash.template. It supports helpers, partials, and complex expressions, making it suitable for more complex templating tasks.
mustache
Mustache is a logic-less templating syntax that can be used for HTML, config files, source code, etc. It is simpler than lodash.template and does not allow for logic within the templates, focusing on simple tag replacement.
ejs
EJS, or Embedded JavaScript templates, is a templating engine that allows developers to generate HTML markup with plain JavaScript. It offers similar functionality to lodash.template but with different syntax and additional features like partials and layouts.
pug
Formerly known as Jade, Pug is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It offers a robust feature set including includes, inheritance, and mixins, which are not present in lodash.template.
lodash.template v3.6.2
The modern build of lodash’s _.template
exported as a Node.js/io.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.template
In Node.js/io.js:
var template = require('lodash.template');
See the documentation or package source for more details.